package cjq.demo.thread.ch6_5;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/5/4.
 */
public class Event implements Delayed {
    private Date startDate;

    public Event(Date startDate){
        this.startDate = startDate;
    }

    public int compareTo(Delayed o){
        long result = this.getDelay(TimeUnit.NANOSECONDS)-o.getDelay(TimeUnit.NANOSECONDS);
        if(result<0){
            return -1;
        }else if(result>0){
            return 1;
        }
        return 0;
    }

    public long getDelay(TimeUnit unit){
        Date now = new Date();
        long diff = startDate.getTime()-now.getTime();
        return  unit.convert(diff,TimeUnit.MILLISECONDS);
    }
}
